home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / alpha / examples / playpaula.d < prev    next >
Encoding:
Text File  |  2002-10-28  |  5.5 KB  |  173 lines

  1. /************************************************************************
  2. *                 *
  3. * playpaula.d  V1.2  06.09.2000  by Rainer "No.3" Müller          *
  4. *                 *
  5. *  an example how to use audio-device with the Amiga E Language   *
  6. *                 *
  7. *  if the file is bigger than 128 KByte, double buffering is used *
  8. *                 *
  9. *  compile: dc playpauly            *
  10. *                 *
  11. *  use: playpaula name      name = name of the file you want to replay  *
  12. *                 *
  13. *  new in:                *
  14. *                 *
  15. *    V1.0  08.01.1999  first version          *
  16. *                 *
  17. *    V1.2  06.09.2000  uses now ReadArgs(), checks for OS2.0+           *
  18. *                 *
  19. ************************************************************************/
  20. OPT OPTIMIZE=-1
  21.  
  22. MODULE 'lib/amiga','devices/audio','dos/dos','dos/rdargs','exec/io','exec/memory','exec/nodes','exec/ports','graphics/gfxbase'
  23.  
  24. ENUM ER_NONE, ER_NOOPEN, ER_NOMEM, ER_NOPORT, ER_NOIOREQ, ER_NODEVICE, ER_NOLOAD, ER_BADARGS, ER_KICK
  25.  
  26. CONST NTSC_CLOCK=3579545,
  27.       PAL_CLOCK=3546895
  28.  
  29. DEF clock
  30.  
  31. PROC main()
  32. DEF fileptr=NIL
  33. DEF dataptr=NIL:PTR TO CHAR
  34. DEF datalen=NIL
  35. DEF rdargs=NIL:PTR TO RDArgs
  36. DEF myargs
  37. DEF gfx:PTR TO GfxBase
  38.  
  39.   gfx:=GfxBase
  40.   IF gfx.DisplayFlags & PAL THEN clock:=PAL_CLOCK ELSE clock:=NTSC_CLOCK
  41.   IF KickVersion(37)=NIL THEN Raise(ER_KICK)
  42.   IF (rdargs:=ReadArgs('File/A', &myargs,NIL))=NIL THEN Raise(ER_BADARGS)
  43.   IF (fileptr:=Open(myargs,  MODE_OLDFILE))=NIL THEN Raise(ER_NOOPEN)
  44.   datalen:=FileLength(myargs)
  45.   IF (dataptr:=AllocVec(datalen,MEMF_ANY))=NIL THEN Raise(ER_NOMEM)
  46.   IFN Read(fileptr, dataptr,  datalen) = datalen THEN Raise(ER_NOLOAD)
  47.  
  48.   playSample(dataptr, datalen)
  49.  
  50. EXCEPTDO
  51.   IF dataptr THEN FreeVec(dataptr)
  52.   IF fileptr THEN Close(fileptr)
  53.   SELECT exception
  54.     CASE ER_KICK;    WriteF('Kickstart V37+ required\n')
  55.     CASE ER_BADARGS; WriteF('bad args\n')
  56.     CASE ER_NOOPEN;  WriteF('could not open file\n')
  57.     CASE ER_NOMEM;   WriteF('no memory\n')
  58.     CASE ER_NONE;    WriteF('all OK\n')
  59.   ENDSELECT
  60. ENDPROC
  61.  
  62. PROC playSample(ptr,len)
  63. DEF arequest1=NIL:PTR TO IOAudio
  64. DEF arequest2:IOAudio
  65. DEF reply1=NIL:PTR TO MP
  66. DEF reply2=NIL:PTR TO MP
  67. DEF ioa=NIL:PTR TO IOAudio
  68. DEF mnode=NIL:PTR TO MN
  69. DEF chipbuf1=NIL:PTR TO CHAR
  70. DEF chipbuf2=NIL:PTR TO CHAR
  71. DEF deviceerror=1
  72. DEF offset
  73. DEF times, rest, go=0
  74. DEF samrate=17000
  75. DEF volume=64   -> Paula's volume range is from 0 (=no volume) to 64 (max volume)
  76.  
  77.   IF (reply1:=CreateMsgPort())=NIL THEN Raise(ER_NOPORT)
  78.   IF (reply2:=CreateMsgPort())=NIL THEN Raise(ER_NOPORT)
  79.   IF (arequest1:=CreateIORequest(reply1, SIZEOF_IOAudio))=NIL THEN Raise(ER_NOIOREQ)
  80.  
  81.   arequest1.IO.MN.LN.Pri:=ADALLOC_MAXPREC  -> tell OpenDevice() to allocate one channel
  82.   arequest1.IO.Command:=ADCMD_ALLOCATE
  83.   arequest1.IO.Flags:=ADIOF_NOWAIT
  84.   arequest1.AllocKey:=0
  85.   arequest1.Data:=[1,2,4,8]:CHAR
  86.   arequest1.Length:=4;
  87.   IF deviceerror:=OpenDevice('audio.device', 0, arequest1, 0) THEN Raise(ER_NODEVICE)
  88.  
  89.   arequest1.IO.Command:=CMD_WRITE    -> copy iorequest for double buffered operation
  90.   arequest1.IO.Flags:=ADIOF_PERVOL
  91.   arequest1.Period:=Div(clock, samrate)
  92.   arequest1.Volume:=volume
  93.   arequest1.Cycles:=1
  94.   CopyMemQuick(arequest1,arequest2, SIZEOF_IOAudio)
  95.  
  96.   IF len >= 131072 -> sample is bigger than 128 KByte => we have to use double buffering
  97.     IF (chipbuf1:=AllocVec(8192, MEMF_CHIP|MEMF_PUBLIC))=NIL THEN Raise(ER_NOMEM)
  98.     IF (chipbuf2:=AllocVec(8192, MEMF_CHIP|MEMF_PUBLIC))=NIL THEN Raise(ER_NOMEM)
  99.  
  100.     times:=Div(len, 8192)
  101.     rest:=len-Mul(times, 8192)
  102.     times---
  103.     mnode:=arequest2
  104.     mnode.ReplyPort:=reply2
  105.  
  106.     CopyMemQuick(ptr, chipbuf1, 8192)       -> send first two buffer's loaded with data to the audio-device
  107.     offset:=8192
  108.     CopyMemQuick(ptr+offset, chipbuf2, 8192)
  109.     offset:=offset + 8192
  110.     arequest1.Data:=chipbuf1
  111.     arequest1.Length:=8192
  112.     arequest2.Data:=chipbuf2
  113.     arequest2.Length:=8192
  114.     BeginIO(arequest1)
  115.     BeginIO(arequest2)
  116.  
  117.     mnode:=arequest1      -> wait on request1 first time around
  118.  
  119.     REPEAT        -> until done, keep feeding data to device
  120.       WaitPort(mnode.ReplyPort)
  121.       GetMsg(mnode.ReplyPort)
  122.       ioa:=mnode
  123.       IF ioa=arequest1
  124.         mnode:=arequest2
  125.       ELSE
  126.         mnode:=arequest1
  127.       ENDIF
  128.       IFN times=0
  129.         CopyMemQuick(ptr+offset, ioa.Data, 8192)
  130.         ioa.Length:=8192
  131.         BeginIO(ioa)
  132.         offset:=offset+8192
  133.         DEC times
  134.       ELSEIF times=0 AND rest<>0
  135.         CopyMemQuick(ptr+offset, ioa.Data, rest)
  136.         ioa.Length:=rest
  137.         BeginIO(ioa)
  138.         rest:=0
  139.       ELSE
  140.         go:=1
  141.         WaitPort(mnode.ReplyPort)
  142.         GetMsg(mnode.ReplyPort)
  143.       ENDIF
  144.     UNTIL go=1
  145.   ELSE
  146.     IF (chipbuf1:=AllocVec(len, MEMF_CHIP|MEMF_PUBLIC))=NIL THEN Raise(ER_NOMEM)
  147.  
  148.      mnode:=arequest1
  149.      mnode.ReplyPort:=reply1
  150.      CopyMemQuick(ptr, chipbuf1, len)       -> copy sample data to chip-memory
  151.      arequest1.Data:=chipbuf1
  152.      arequest1.Length:=len
  153.      BeginIO(arequest1)
  154.      WaitPort(mnode.ReplyPort)
  155.      GetMsg(mnode.ReplyPort)
  156.   ENDIF
  157.  
  158. EXCEPTDO
  159.   IF chipbuf2 THEN FreeVec(chipbuf2)
  160.   IF chipbuf1 THEN FreeVec(chipbuf1)
  161.   IF deviceerror=0 THEN CloseDevice(arequest1)
  162.   IF arequest1 THEN DeleteIORequest(arequest1)
  163.   IF reply2 THEN DeleteMsgPort(reply2)
  164.   IF reply1 THEN DeleteMsgPort(reply1)
  165.   SELECT exception
  166.      CASE ER_NOMEM   ; WriteF('no memory\n')
  167.      CASE ER_NOPORT  ; WriteF('could not create port\n')
  168.      CASE ER_NOIOREQ ; WriteF('could not create iorequest\n')
  169.      CASE ER_NODEVICE; WriteF('could not open audio-device\n')
  170.      CASE ER_NONE    ; WriteF('all OK\n')
  171.   ENDSELECT
  172. ENDPROC
  173.